package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Role;
import com.example.demo.entity.RoleMenu;
import com.example.demo.entity.UserRole;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.UserRoleMapper;
import com.example.demo.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/18 19:58
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Autowired(required = false)
    UserRoleMapper user_roleMapper;
    @Autowired(required = false)
    RoleMapper roleMapper;
//    @Override
//    public List<User_Role> SelectUser_Role(Integer user_id) {
//        QueryWrapper<User_Role> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("user_id",user_id);
//        List<User_Role> User_Roles = user_roleMapper.selectList(queryWrapper);
//        return User_Roles;
//    }

    @Override
    public void InsertUser_Role(Integer user_id, Integer role_id) {
        UserRole user_role = new UserRole(user_id, role_id);
        this.save(user_role);
    }

    @Override
    public void DeleteUser_Role(Integer user_id, Integer role_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("role_id", role_id);
        user_roleMapper.deleteByMap(map);
    }

    @Override
    public void DeleteUser_RoleByRole(Integer role_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("role_id", role_id);
        user_roleMapper.deleteByMap(map);
    }

    @Override
    public void DeleteUser_RoleByUser(Integer user_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", user_id);
        user_roleMapper.deleteByMap(map);
    }

    @Override
    public List<UserRole> SelectUser_RoleByUser(Integer user_id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", user_id);
        List<UserRole> user_Roles = user_roleMapper.selectList(queryWrapper);
        return user_Roles;
    }

    @Override
    public List<UserRole> SelectUser_RoleByRole(Integer role_id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", role_id);
        List<UserRole> user_Roles = user_roleMapper.selectList(queryWrapper);
        return user_Roles;
    }

    @Override
    public List<Role> SelectRoleByUser(Integer user_id) {
        List<UserRole> userRoles = this.SelectUser_RoleByUser(user_id);
        List<Integer> Role_id = userRoles.stream().map(UserRole::getRole_id).collect(Collectors.toList());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("role_id", Role_id);
        List<Role> list = roleMapper.selectList(wrapper);
        return list;
    }
}
